Author: SAI K
Core Java
In Java, access modifiers are keywords that set the accessibility of classes, methods, and other members. They control where the members can be accessed from and ensure encapsulation. Java provides four main access modifiers: public, private, protected, and default (no modifier).
Access modifiers in Java define the scope of accessibility of a class, method, constructor, or variable. They help in implementing the principle of encapsulation by restricting access to certain members from outside the class or package.
Java provides four main types of access modifiers:
The public access modifier makes a class, method, or variable accessible from any other class. There are no restrictions on its visibility.
public class PublicExample {
public int data = 10;
public void display() {
System.out.println("Public method");
}
public static void main(String[] args) {
PublicExample obj = new PublicExample();
System.out.println(obj.data); // Accessible
obj.display(); // Accessible
}
}
The private access modifier restricts the access of a class, method, or variable to within the same class only. It is not accessible from any other class.
public class PrivateExample {
private int data = 10;
private void display() {
System.out.println("Private method");
}
public static void main(String[] args) {
PrivateExample obj = new PrivateExample();
System.out.println(obj.data); // Accessible
obj.display(); // Accessible
}
}
The protected access modifier allows access to the members of a class within the same package and by subclasses in different packages.
public class ProtectedExample {
protected int data = 10;
protected void display() {
System.out.println("Protected method");
}
}
class Subclass extends ProtectedExample {
public static void main(String[] args) {
Subclass obj = new Subclass();
System.out.println(obj.data); // Accessible
obj.display(); // Accessible
}
}
When no access modifier is specified, it is known as the default access modifier. It allows access to the members of a class within the same package only.
class DefaultExample {
int data = 10; // default access modifier
void display() {
System.out.println("Default method");
}
public static void main(String[] args) {
DefaultExample obj = new DefaultExample();
System.out.println(obj.data); // Accessible
obj.display(); // Accessible
}
}
public class PublicClass {
public int data = 100;
public void showData() {
System.out.println("Public Data: " + data);
}
}
class TestPublic {
public static void main(String[] args) {
PublicClass obj = new PublicClass();
obj.showData(); // Accessible
}
}
class PrivateClass {
private int data = 200;
private void showData() {
System.out.println("Private Data: " + data);
}
public static void main(String[] args) {
PrivateClass obj = new PrivateClass();
obj.showData(); // Accessible within the same class
}
}
class ProtectedClass {
protected int data = 300;
protected void showData() {
System.out.println("Protected Data: " + data);
}
}
class SubClassProtected extends ProtectedClass {
public static void main(String[] args) {
SubClassProtected obj = new SubClassProtected();
obj.showData(); // Accessible in subclass
}
}
class DefaultClass {
int data = 400;
void showData() {
System.out.println("Default Data: " + data);
}
}
class TestDefault {
public static void main(String[] args) {
DefaultClass obj = new DefaultClass();
obj.showData(); // Accessible within the same package
}
}
Access modifiers in Java are crucial for encapsulation and defining the visibility of classes, methods, and variables. Understanding how to use public, private, protected, and default access modifiers allows developers to effectively control access to the components of their code, enhancing security and maintainability.